﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace quanlysinhvien
{
    
    public partial class FormInfo_Student : Form
    {
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataReader rd;
        public static string student_code;
        public FormInfo_Student()
        {
            InitializeComponent();
        }

        
        private void FormInfo_Student_Load(object sender, EventArgs e)
        {
            conn = Connect.getConnection();
            try
            {
                conn.Open();
                String strsql = "select Student_Code,Class_Code,Faculty_Name,Student_Name,Student_DOB,Student_Sex,Current_Address,Native_Place,Contact_Address,Father_Name,Father_Job,Mother_Name,Mother_Job from TblStudents where Student_Code='" + FrmListStudent.studentcode + "'";
                cmd = new SqlCommand(strsql, conn);


                rd = cmd.ExecuteReader();
                if (rd.Read())
                {
                    txtStuCode.Text = rd.GetString(0);
                    txtClaCode.Text = rd.GetString(1);
                    txtFacName.Text = rd.GetString(2);
                    txtStuName.Text = rd.GetString(3);
                    dateDOB.Text = rd.GetDateTime(4).ToString();
                    comboSex.Text = rd.GetString(5);
                    txtCurAdd.Text = rd.GetString(6);
                    txtNatPlace.Text = rd.GetString(7);
                    txtConAdd.Text = rd.GetString(8);
                    txtFatName.Text = rd.GetString(9);
                    txtFatJob.Text = rd.GetString(10);
                    txtMotName.Text = rd.GetString(11);
                    txtMotJob.Text = rd.GetString(12);

                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
            conn.Close();


        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            conn = Connect.getConnection();
            try
            {
                if (conn != null)
                {
                    
                    conn.Open();
                    String strsql = "update TblStudents set Student_Code='" + txtStuCode.Text + "',Class_Code='" + txtClaCode.Text + "',Faculty_Name=N'" + txtFacName.Text + "',Student_Name=N'" + txtStuName.Text + "',Student_DOB='" + DateTime.Parse(dateDOB.Text) + "',Student_Sex='" + comboSex.Text + "',Current_Address=N'" + txtCurAdd.Text + "',Native_Place=N'" + txtNatPlace.Text + "',Contact_Address=N'" + txtConAdd.Text + "',Father_Name=N'" + txtFatName.Text + "',Father_Job=N'" + txtFatJob.Text + "',Mother_Name=N'" + txtMotName.Text + "',Mother_Job=N'" + txtMotJob.Text + "' where Student_Code='" + FrmListStudent.studentcode + "'";
                    cmd = new SqlCommand(strsql, conn);
                    rd = cmd.ExecuteReader();
                    conn.Close();
                    MessageBox.Show("Update successful!", "Successful!");


                }
            }
            catch (Exception)
            {
                MessageBox.Show("Input again", "Update error");
            }

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void btnInfo_Click(object sender, EventArgs e)
        {
            student_code = txtStuCode.Text.ToString();
            FrmResult FrmResult = new FrmResult();
            FrmResult.Show();

        }


       
    }
}
